package com.ztw.yygh.cmn.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.ztw.yygh.cmn.Utils.HospitalUtil;
import com.ztw.yygh.cmn.service.DepartmentService;
import com.ztw.yygh.cmn.service.HospitalService;
import com.ztw.yygh.cmn.service.ScheduleService;
import com.ztw.yygh.common.result.Result;
import com.ztw.yygh.common.utils.HttpRequestHelper;
import com.ztw.yygh.model.hosp.Schedule;
import com.ztw.yygh.vo.hosp.ScheduleQueryVo;
import io.lettuce.core.models.role.RedisSlaveInstance;
import org.springframework.data.domain.*;
import com.ztw.yygh.model.hosp.Department;
import com.ztw.yygh.model.hosp.Hospital;
import com.ztw.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private HospitalService hospitalService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private HospitalUtil hospitalUtil;
    @Resource
    private ScheduleService scheduleService;


    //删除排班接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        String hoscode = (String) parameterMap.get("hoscode");
        String hosScheduleId = (String) parameterMap.get("hosScheduleId");
        hospitalUtil.CheckKey(parameterMap, hoscode);
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        String hoscode = (String) parameterMap.get("hoscode");
        hospitalUtil.CheckKey(parameterMap, hoscode);
        scheduleService.save(parameterMap);
        return Result.ok();
    }
    //查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        String hoscode =(String)parameterMap.get("hoscode");
        String depcode =(String)parameterMap.get("depcode");
        //页码 页数
        Integer page=StringUtils.isEmpty(parameterMap.get("page"))?1:Integer.parseInt((String) parameterMap.get("page"));
        Integer limit=StringUtils.isEmpty(parameterMap.get("limit"))?1:Integer.parseInt((String) parameterMap.get("limit"));
        hospitalUtil.CheckKey(parameterMap,hoscode);
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel=scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }
    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {

        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        String hoscode = (String) parameterMap.get("hoscode");
        String depcode = (String) parameterMap.get("depcode");
        hospitalUtil.CheckKey(parameterMap, hoscode);
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }
    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        String hoscode =(String)parameterMap.get("hoscode");
        //页码 页数
        Integer page=StringUtils.isEmpty(parameterMap.get("page"))?1:Integer.parseInt((String) parameterMap.get("page"));
        Integer limit=StringUtils.isEmpty(parameterMap.get("limit"))?1:Integer.parseInt((String) parameterMap.get("limit"));
        hospitalUtil.CheckKey(parameterMap,hoscode);
        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel=departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }
    //查询医院
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        //签名验证
        String hoscode =(String)parameterMap.get("hoscode");
        hospitalUtil.CheckKey(parameterMap,hoscode);
        Hospital hospital=hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        //签名验证
        String hoscode =(String)parameterMap.get("hoscode");
        hospitalUtil.CheckKey(parameterMap,hoscode);
        departmentService.save(parameterMap);
        return Result.ok();
    }
    //上传医院
    @PostMapping("/saveHospital")
    public Result saveHost(HttpServletRequest request){
        Map<String, String[]> stringMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(stringMap);
        //签名验证
        String hoscode =(String)parameterMap.get("hoscode");
        hospitalUtil.CheckKey(parameterMap,hoscode);
        //服务器过程中“+”->“ ”
        String logoData=(String)parameterMap.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        parameterMap.put("logoData",logoData);
        hospitalService.save(parameterMap);
        return Result.ok();
    }
}
